using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.OracleClient;
using System.Collections.Generic;
using Pms.Utils;
using Biblioteca.Utils;
using Biblioteca.DataBase;
/*
 * 
 * Autor      : Jose Jornando de Carvalho Junior    
 * Data       : 10/04/2010 07:06:56
 * Descricao  : 
 * Alteracoes : 
 * 
 */

namespace SERIX
{
   public class CGrausParentescos
   {
       private string IDT_GRAU ;
       private string GRAU_UNICO ;
       private string DSC_GRAU ;

	   /// <sumary>
       /// Construtor da classe graus_parentescos
       /// <sumary>
       public CGrausParentescos()
       {
   
       }

	   /// <sumary>
       /// Construtor da classe Graus_parentescos
       /// <sumary>
       public CGrausParentescos( string IDT_GRAU, string GRAU_UNICO, string DSC_GRAU )
       {
          this.IDT_GRAU = IDT_GRAU;
          this.GRAU_UNICO = GRAU_UNICO;
          this.DSC_GRAU = DSC_GRAU;
       }
	   
	   #region Propriedades
     	/// <sumary>
        /// Identificador do grau de parentesco
        /// <sumary>
        public string Idt_grau 
		{
		   get
		   {
		       return this.IDT_GRAU;
		   }
		   set
		   {
		       this.IDT_GRAU = value;
		   }
		}
     	/// <sumary>
        /// Indica que somente e permitido um beneficiario com o grau de parentesco informado no cadatro de familias do beneficiarios (tabela familias_beneficiarios)

        /// <sumary>
        public string Grau_unico 
		{
		   get
		   {
		       return this.GRAU_UNICO;
		   }
		   set
		   {
		       this.GRAU_UNICO = value;
		   }
		}
     	/// <sumary>
        /// Descricao do grau de parentesco
        /// <sumary>
        public string Dsc_grau 
		{
		   get
		   {
		       return this.DSC_GRAU;
		   }
		   set
		   {
		       this.DSC_GRAU = value;
		   }
		}
	    #endregion Propriedades
	
	    /// <summary>
        /// Inserindo um novo Graus_parentescos.
        /// </summary>
        public void Inserir(string Login)
        {
		    OracleCommand command = new OracleCommand();
            OracleTransaction trans;

            string SQL = @"insert into Serix.SRX_GRAUS_PARENTESCOS
			                  ( IDT_GRAU, GRAU_UNICO, DSC_GRAU )
						   values
						      ( :IDT_GRAU, :GRAU_UNICO, :DSC_GRAU )";
     		
			try
			{	
				command.Connection = BiblioPMS.ObterConexao(BiblioPMS.aplicacaoSNG, Login, BiblioPMS.Demo);
                trans = command.Connection.BeginTransaction();
                command.Transaction = trans;
                command.CommandType = CommandType.Text;
                command.CommandText = SQL;
				
				command.Parameters.Add(new OracleParameter("IDT_GRAU",OracleType.Number)); 
				command.Parameters.Add(new OracleParameter("GRAU_UNICO",OracleType.Char)); 
				command.Parameters.Add(new OracleParameter("DSC_GRAU",OracleType.VarChar)); 
				
                command.Parameters["IDT_GRAU"].Value = this.IDT_GRAU;
                command.Parameters["GRAU_UNICO"].Value = this.GRAU_UNICO;
                command.Parameters["DSC_GRAU"].Value = this.DSC_GRAU;
				command.ExecuteNonQuery();
                command.Parameters.Clear();
                command.Transaction.Commit();
            }
            catch (Exception e)
            {
                command.Transaction.Rollback();

                if (e is OracleException)
                    throw new Exception(Biblio.ObterMensagem(((OracleException)e).Code, e.Message));
                else
                    throw new Exception(e.Message + " : " + "");
            }
		}
		
	    /// <summary>
        /// Alterando um Graus_parentescos.
        /// </summary>
        public void Alterar(string Login)
        {
		    OracleCommand command = new OracleCommand();
            OracleTransaction trans;

            string SQL = @"update Serix.SRX_GRAUS_PARENTESCOS set 
			                   GRAU_UNICO = :GRAU_UNICO, DSC_GRAU = :DSC_GRAU
						   where
						       IDT_GRAU = :IDT_GRAU ";
     		
			try
			{	
				command.Connection = BiblioPMS.ObterConexao(BiblioPMS.aplicacaoSNG, Login, BiblioPMS.Demo);
                trans = command.Connection.BeginTransaction();
                command.Transaction = trans;
                command.CommandType = CommandType.Text;
                command.CommandText = SQL;
				
				command.Parameters.Add(new OracleParameter("IDT_GRAU",OracleType.Number)); 
				command.Parameters.Add(new OracleParameter("GRAU_UNICO",OracleType.Char)); 
				command.Parameters.Add(new OracleParameter("DSC_GRAU",OracleType.VarChar)); 
				
                command.Parameters["IDT_GRAU"].Value = this.IDT_GRAU;
                command.Parameters["GRAU_UNICO"].Value = this.GRAU_UNICO;
                command.Parameters["DSC_GRAU"].Value = this.DSC_GRAU;
				command.ExecuteNonQuery();
                command.Parameters.Clear();
                command.Transaction.Commit();
            }
            catch (Exception e)
            {
                command.Transaction.Rollback();

                if (e is OracleException)
                    throw new Exception(Biblio.ObterMensagem(((OracleException)e).Code, e.Message));
                else
                    throw new Exception(e.Message + " : " + "");
            }
		}
		
	     /// <summary>
        /// Exclui um  Graus_parentescos.
        /// </summary>
        public void Excluir(string Login)
        {
            Tabela tab = new Tabela("SRX_GRAUS_PARENTESCOS", BiblioPMS.ObterConexao(BiblioPMS.aplicacaoSNG, Login,BiblioPMS.Demo));
            tab.adicionarFiltro(new Coluna("IDT_GRAU", DbType.Int32, this.IDT_GRAU));
            tab.delete();
        }
		
		static public DataTable ObterTodos(string login)
        {
            
            OracleCommand command = new OracleCommand();

            string SQL = @"select * from Serix.SRX_GRAUS_PARENTESCOS order by DSC_GRAU ";

            try
            {
                command.Connection = BiblioPMS.ObterConexao(BiblioPMS.aplicacaoSNG, login, false);
                command.CommandType = CommandType.Text;
                command.CommandText = SQL;

                DataTable tbl = new DataTable();
                tbl.Load(command.ExecuteReader(CommandBehavior.CloseConnection)); 
                return tbl; 
                
            }
            catch (Exception e)
            {
 
                if (e is OracleException)
                    throw new Exception(Biblio.ObterMensagem(((OracleException)e).Code, e.Message));
                else
                    throw new Exception(e.Message + " : " + "");
            }

        }
		
		static public CGrausParentescos PesquisaporCodigo(string idt_grau, string login)
        {
            CGrausParentescos obj;
            string SQL = @"SELECT *
                            FROM  Serix.SRX_GRAUS_PARENTESCOS 
                            WHERE IDT_GRAU = :IDT_GRAU";
            Query qy = null;
            try
            {
                qy = new Query(BiblioPMS.ObterConexao(BiblioPMS.aplicacaoSNG, login, BiblioPMS.Demo), SQL);
				qy.AdicionarParametro(":IDT_GRAU", DbType.String, idt_grau);
               
				qy.Executar();
                obj = new CGrausParentescos( qy.ObterCampo("IDT_GRAU"), qy.ObterCampo("GRAU_UNICO"), qy.ObterCampo("DSC_GRAU") );
            }                                  
            catch
            {
                if (qy.EstadoConexao() == ConnectionState.Open)
                {
                    qy.FecharConexao();
                }
                return new CGrausParentescos();
            }
			finally
            {
                qy.FecharConexao();
            }
            return obj;
        }
		
		static public int ObterNovoIdt(string login)
        {
            string SQL = @"SELECT PKG_BIBSERIX.OBTER_IDT_GRAU() IDT From dual ";
            Query qy = null;
            try
            {
                qy = new Query(BiblioPMS.ObterConexao(BiblioPMS.aplicacaoSNG, login, BiblioPMS.Demo), SQL);
				qy.Executar();
				return Convert.ToInt32(qy.ObterCampo("IDT"));
            }
            catch
            {
                if (qy.EstadoConexao() == ConnectionState.Open)
                {
                    qy.FecharConexao();
                }
                return-1;
            }
			finally
            {
                qy.FecharConexao();
            }
        }
		
    }
}	

